<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>addSeed</title>
    <style>
        .form-control {
            height: 35px;
        }

        .form-control-feedback{
            right: 25px;
        }

        .glyphicon-ok{
            color:green;
        }
        .glyphicon-remove{
            color:#A94442;
        }

    </style>
</head>
<body>
<div role="seed" >
    <div class="panel panel-default">
        <div class="panel-body">
            <form class="form-horizontal"  action="/crawl/seed/saveSeed"  role="seed">
                <fieldset>
                    <div class="control-group" style="margin-top: 25px;">
                        <!-- Text input-->
                        <label class="col-sm-2 control-label">爬取起始地址</label>
                        <div class="col-sm-10">
                            <input role="seedpath" name="seedpath" type="text" placeholder="请输入爬取起始地址" class="form-control"/>
                            <input role="planid4seed"  name="planid" type="hidden" />
                            <input role="seedid"  name="seedid" type="hidden" />
                        </div>
                    </div>

                    <div class="control-group" style="margin-top: 75px;">
                        <!-- Textarea -->
                        <label class="col-sm-2 control-label">爬取起始地址描述</label>
                        <div class="col-sm-10">
                            <div class="textarea">
                                <textarea name="seedpathdesc" role="seedpathdesc" class="form-control"> </textarea>
                            </div>
                        </div>
                    </div>
                </fieldset>
                <div style="margin-top:20px;">
                    <button id="submitBtn4seed" onclick="saveSeed()"  class="btn btn-success" style="display: inline-block;margin-left: 22%;" type="button" >保  存</button>
                    <button type="button" class="btn" onclick="javascript: $('a.close').click();" style="display: inline-block;margin-left: 72%;margin-top: -56px;"   >取  消</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $("input[role = 'planid4seed']").val(  $("#planid").val() );
        if(seedid != ""){
            setupSeedForm(seedid);
        }
        $("form[role='seed']").bootstrapValidator({
            /**
             * 为每个字段指定通用错误提示语
             */
            message: '当前值验证不通过!',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                seedpathdesc: {
                    validators: {
                        notEmpty: {
                            message: '爬取起始地址描述不能为空!'
                        }
                    }
                },
                seedpath: {
                    validators: {
                        notEmpty: {
                            message: '爬取起始地址不能为空!'
                        },
                        uri: {
                            allowLocal: true,
                            message: '请输入正确的url地址!'
                        }
                    }
                }
            },
            /**
             *  指定不验证的情况
             *  值可设置为以下三种类型：
             *  1、String  ':disabled, :hidden, :not(:visible)'
             *  2、Array  默认值  [':disabled', ':hidden', ':not(:visible)']
             *  3、带回调函数
             [':disabled', ':hidden', function($field, validator) {
             $field 当前验证字段dom节点
             validator 验证实例对象
             可以再次自定义不要验证的规则
             必须要return，return true or false;
            return !$field.is(':visible');
            }]
             */
            excluded: [':disabled', ':hidden', ':not(:visible)'],
            /**
             * 指定提交的按钮，例如：'.submitBtn' '#submitBtn'
             * 当表单验证不通过时，该按钮为disabled
             */
            submitButtons: '#submitBtn4seed',
            /**
             * 生效规则（三选一）
             * enabled 字段值有变化就触发验证
             * disabled,submitted 当点击提交时验证并展示错误信息
             */
            live: 'enabled'
            /**
             * submitHandler: function(validator, form, submitButton) {
            *   validator: 表单验证实例对象
            *   form  jq对象  指定表单对象
            *   指定表单对象submitButton  jq对象  指定提交按钮的对象
            * }
             * 在ajax提交表单时很实用
             *   submitHandler: function(validator, form, submitButton) {
                实用ajax提交表单
                $.post(form.attr('action'), form.serialize(), function(result) {
                   自定义回调逻辑
                }, 'json');
            }
             *
             */
        });
    });

    function saveSeed() {
        /*获取表单对象*/
        var bootstrapValidator = $("form[role='seed']").data('bootstrapValidator');
        /*手动触发验证*/
        bootstrapValidator.validate();
        if(bootstrapValidator.isValid()){
           /* 表单提交的方法、比如ajax提交*/
            $.post($("form[role='seed']").attr('action'), $("form[role='seed']").serialize(), function(result) {
                if(result.isSaved == "ok"){
                    $.scojs_message('保存爬取起始地址成功!', $.scojs_message.TYPE_OK);
                }else{
                    $.scojs_message('保存爬取起始地址失败!', $.scojs_message.TYPE_ERROR);
                }
            }, 'json');
        }
    }

    function setupSeedForm(seedid) {
        $.ajax({
            url:'/crawl/seed/getSeedById?seedid='+seedid,
            async:false,
            success:function(data){
                $("input[role='seedid']").val(data.seedid);
                $("input[role='seedpath']").val(data.seedpath);
                $("textarea[role='seedpathdesc']").val(data.seedpathdesc);
            }
        });
    }
</script>
</body>

</html>